var express = require('express');
var router = express.Router();
var menuService = require('../service/menuService')
var userMenusService = require('../service/userMenusService')
var typesService = require('../service/typesService')

router.get('/query', async function (req, res, next) {
    let { userId } = req.query
    let result = await menuService.query({ userId });
    res.json({
        code: 200,
        data: result
    })
});

//新增数据
router.post('/add', async function (req, res, next) {
    let { userId, name, category } = req.body
    let result = await menuService.query({ name });
    if (result.length > 0) {
        return res.json({
            code: 400,
            msg: '该菜单已存在'
        })
    }
    let result2 = await menuService.add({ userId, name, category })
    res.json({
        code: 200,
        data: result2
    })
});

//新增数据到userMenu
router.post('/usermenusadd', async function (req, res, next) {
    let { list, typeid } = req.body
    // console.log(list, typeid);
    await userMenusService.delMany({ typeid })
    let result = await userMenusService.add(list)
    res.json({
        code: 200,
        data: result
    })
});

//查询数据到userMenu
router.get('/usermenusquery', async function (req, res, next) {
    let typeid = req.query
    console.log(typeid);
    let result = await userMenusService.query(typeid)
    res.json({
        code: 200,
        data: result
    })
});

module.exports = router;
